package model;

import java.awt.Toolkit;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.util.Date;

public class IOFile {
	private static final int BUFFER_SIZE = 10240;
	private File errorFile;
	private PrintWriter pw;
	private volatile static IOFile instance;
	private Date date;

	private IOFile() {
		date = new Date();
		errorFile = new File("./data/error.sm");
		if (!errorFile.getParentFile().exists()) {
			errorFile.getParentFile().mkdirs();
		}
		try {
			this.pw = new PrintWriter(new BufferedOutputStream(
					new FileOutputStream(errorFile, true)), true);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	public static PrintWriter getInstance() {
		if (instance == null) {
			synchronized (IOFile.class) {
				if (instance == null) {
					instance = new IOFile();
				}
			}
		}
		return instance.getPrintStream();
	}

	private PrintWriter getPrintStream() {
		this.pw.println("\r\n######### " + date);
		return this.pw;
	}

	public static boolean createParentFile(String path) {
		File file = new File(path);
		if (!file.getParentFile().exists())
			return file.getParentFile().mkdirs();
		return true;
	}

	public static void copyFile(InputStream is, OutputStream os)
			throws IOException {
		byte[] buffer = new byte[BUFFER_SIZE];
		int len = 0;
		while ((len = is.read(buffer)) != -1) {
			os.write(buffer, 0, len);
		}
		is.close();
		os.close();
	}

	public static void createFontFile() throws IOException {
		createParentFile("./data/fonts/");
		FileOutputStream fos = new FileOutputStream(
				"./data/fonts/SNAP_ITC_REGULA.TTF");
		IOFile.copyFile(Toolkit.getDefaultToolkit().getClass()
				.getResourceAsStream("/fonts/SNAP_ITC_REGULA.TTF"), fos);
	}

	public static void main(String[] args) {
		try {
			Long.parseLong("afdasdf");
		} catch (Exception e) {
			e.printStackTrace(IOFile.getInstance());
			System.out.println("done");
		}
	}
}
